package com.maxtech.basestation;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.provider.Settings;
import android.widget.Toast;

public class Utils {
	public static void Toast(Context context, String str) {
		Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
	}
	
    /** 
     * 检测网络连接是否可用 
     * @param context 
     * @return true 可用; false 不可用 
     */  
    public static boolean isNetworkAvailable(Context context) {  
        ConnectivityManager cm =   
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
        if(cm == null) {  
            return false;  
        }  
        NetworkInfo[] netinfo = cm.getAllNetworkInfo();  
        if(netinfo == null) {  
            return false;  
        }  
        for (int i = 0; i < netinfo.length; i++) {  
            if(netinfo[i].isConnected()) {  
                return true;  
            }
        }  
        return false;  
    }  
	//Check Mobile State
    public static boolean CheckMobileState(Context context)
    {
        ConnectivityManager manager = (ConnectivityManager)context.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        
        if(mobile != State.CONNECTED&&mobile==State.CONNECTING)
            return false;
        
        return true;
    }
    
	//Check Wifi State
    public static boolean CheckWifiState(Context context)
    {
        ConnectivityManager manager = (ConnectivityManager)context.getSystemService(
                Context.CONNECTIVITY_SERVICE);

        State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

        if(wifi != State.CONNECTED)
            return false;
        
        return true;        
    }    
    
	//Check network
    public static void CheckNetworkState(Context context)
    {
        boolean flag = false;
        ConnectivityManager manager = (ConnectivityManager)context.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        //如果3G、wifi、2G等网络状态是连接的，则退出，否则显示提示信息进入网络设置界面
        if(mobile == State.CONNECTED||mobile==State.CONNECTING)
        return;
        if(wifi == State.CONNECTED||wifi==State.CONNECTING)
        return;
//        showTips(context);
    }
    
//    private static void showTips(final Context context)
//    {
//        AlertDialog.Builder builder = new AlertDialog.Builder(context);
//        builder.setIcon(android.R.drawable.ic_dialog_alert);
//        builder.setTitle(R.string.netstate);
//        builder.setMessage(R.string.setnetwork);
//        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
//            
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                // 如果没有网络连接，则进入网络设置界面
//            	context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
//            }
//        });
//        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
//            
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.cancel();
//            }
//        });
//        builder.create();
//        builder.show();
//    }
}
